package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.EmployeeConstant;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.service.StatisticsService;
import com.itheima.vo.StaCateVo;
import com.itheima.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Integer> getDish2MealCount() {
        //获取到当前数据库中的菜品和套餐的数量
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getIsDeleted, EmployeeConstant.IS_NOT_DELETED);
        Integer dishCount = dishMapper.selectCount(dishWrapper);
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getIsDeleted, EmployeeConstant.IS_NOT_DELETED);
        Integer setMelCount = setmealMapper.selectCount(setmealWrapper);
        if (dishCount == null || setMelCount == null) {
            return null;
        }
        ArrayList<Integer> counts = new ArrayList<>();
        counts.add(dishCount);
        counts.add(setMelCount);
        return counts;
    }

    @Override
    public List<UserVO> getUserCounts() {
        /*LocalDate now = LocalDate.now();
        LocalDate localDate = now.plusDays(-7);*/
        //根据时间范围获取到人员 根据时间统计个数
        //select count(create_date) from user where create_date between now and localDate
        List<UserVO> userCounts = userMapper.getUserCounts();
        if (userCounts == null) {
            return null;
        }
        return userCounts;
    }

    @Override
    public List<StaCateVo> getCateCount() {
        List<Category> categories = categoryMapper.selectList(null);
        List<StaCateVo> list = categories.stream().map((category -> {
            String name = category.getName();
            Long id = category.getId();
            LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Dish::getCategoryId, id);
            Integer value = dishMapper.selectCount(queryWrapper);
            StaCateVo staCateVo = new StaCateVo();
            staCateVo.setName(name);
            staCateVo.setValue(value);
            return staCateVo;
        })).collect(Collectors.toList());

        return list;
    }
}
